package com.bdqn.t313.test;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * DOM解析XML工具类
 */
public class DomParseXMLUtil {

    private static String xmlPath;      //XML文件路径

    public DomParseXMLUtil(String xmlPath){
        this.xmlPath = xmlPath;
    }

    /**
     * 为xml文档新增节点
     * @param domElement
     */
    public void addElement(DomElement domElement) throws ParserConfigurationException, IOException, SAXException {
        //1、创建解析器工厂实例
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        //2、创建DocumentBuilder对象
        DocumentBuilder db = builderFactory.newDocumentBuilder();
        //3、解析XML文档，得到一个Document对象
        Document document = db.parse(xmlPath);
        //1、创建childElement子节点
        Element childElement = document.createElement(domElement.getElementName());
        childElement.setAttribute(domElement.getAttributeName(),domElement.getAttributeValue());
        //2、创建childElement下的子节点descendantElement节点
        Element descendantElement  = document.createElement(domElement.getChildElementName());
        //设置descendantElement下的文本值
        descendantElement.setTextContent(domElement.getTextContext());
        //3、添加父子关系
        //将descendantElement子节点追加到childElement节点下
        childElement.appendChild(descendantElement);
        //获取rootElement节点
        Element rootElement = (Element) document.getElementsByTagName(domElement.getRootName()).item(0);
        //将childElement节点追加到rootElement节点下
        rootElement.appendChild(childElement);
        transformXML(document);     //保存xml文档
    }

    /**
     * 保存XML文档
     */
    public void transformXML(Document document){
        try {
            //将追加的节点保存到xml文档中
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource domSource = new DOMSource(document);
            transformer.setOutputProperty(OutputKeys.ENCODING,"UTF-8");
            StreamResult result = new StreamResult(new FileOutputStream(xmlPath));
            //将domSource文件写入到result路径
            transformer.transform(domSource,result);
            System.out.println("保存成功！！");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (TransformerException e) {
            e.printStackTrace();
        } catch (Exception ex){
            ex.printStackTrace();
        }
    }
}
